package com.evil.application.listener;

import com.alibaba.fastjson.JSON;
import com.evil.application.annotation.Automation;
import com.evil.application.pojo.dto.mq.ExcelExportMQReqDTO;
import com.evil.application.pojo.dto.order.exp.OrderExportReqDTO;
import com.evil.application.service.AppOrderOperationService;
import com.evil.application.service.ApplicationQueueService;
import com.evil.common.core.entity.QueryModel;
import com.evil.common.rocketmq.constant.MQConstant;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.stereotype.Component;

/**
 * 导入应用订单 队列消费着
 *
 * @author liyang
 * @date 2022-08-08 00:00
 */
@Slf4j
@Component
@AllArgsConstructor
@RocketMQMessageListener(
        topic = MQConstant.EXPORT_APPLICATION_ORDER_MQ_KEY,
        consumerGroup = MQConstant.EXPORT_APPLICATION_ORDER_CONSUMER_GROUP
)
public class ExportAppOrderRocketMqConsumer implements RocketMQListener<ExcelExportMQReqDTO> {

    private final ApplicationQueueService applicationQueueService;

    private final AppOrderOperationService appOrderOperationService;

    @Override
    @Automation
    public void onMessage(ExcelExportMQReqDTO excelExportMQReqDTO) {
        QueryModel<OrderExportReqDTO> queryModel = excelExportMQReqDTO.getQueryModel();
        Long queueId = excelExportMQReqDTO.getQueueId();
        appOrderOperationService.excelExport(queryModel, (queueStatusEnum, queueContentDTO) ->
                applicationQueueService.updateQueueResult(queueId, queueStatusEnum, JSON.toJSONString(queueContentDTO)));
    }
}
